﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.Logging;
using SchEdu.Framework.Validators;
using SchEdu.Framework.DataModel;
using System.Windows.Forms.DataVisualization.Charting;

namespace SchEdu.Framework.Win
{
    public partial class MachineUtilizationChart : UserControl
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        public MachineUtilizationChart()
        {
            InitializeComponent();
        }

        public void DisplayChart(SchEduData schEduData, SchEduDataSet.SolutionRow solution)
        {
            Validate(schEduData, solution);
            AllocationList allocations = new AllocationList(solution.GetAllocationRows());
            Dictionary<System.Guid,string> machines = new Dictionary<Guid,string>();
            foreach (SchEduDataSet.AllocationRow row in allocations)
            {
                if (machines.ContainsKey(row.MachineGuid) == true)
                {
                    continue;
                }

                machines.Add(row.MachineGuid, row.Machine);
            }

            utilizationChart.ChartAreas.Clear();
            utilizationChart.Series.Clear();
            

            foreach (KeyValuePair<System.Guid,string> machine in machines)
            {
                ChartArea chartArea = new ChartArea(machine.Value);
                utilizationChart.ChartAreas.Add(chartArea);
                Series series = new Series(machine.Value);
                series.ChartArea = chartArea.Name;
                series.BorderWidth = 3;
                chartArea.AxisX.Minimum = 0;
                chartArea.AxisX.Maximum = allocations.GetMakespan();
                chartArea.AxisY.Minimum = 0;
                chartArea.AxisY.Maximum = 2;
                chartArea.AxisY.Interval = 2;
                chartArea.AxisX.Interval = chartArea.AxisX.Maximum / 20;

                series.ChartType = SeriesChartType.StepLine;

                AllocationList machineAllocation = allocations.GetAllocationByMachineGuid(machine.Key);
                machineAllocation.SortByStartDate();
                SchEduDataSet.AllocationRow previousAllocation = null;

                for (int i = 0; i< machineAllocation.Count; previousAllocation = machineAllocation[i], i++)
                {
                    if (previousAllocation == null)
                    {
                        if (machineAllocation[i].StartDate != 0)
                        {
                            series.Points.AddXY(0, 0);
                        }
                        series.Points.AddXY(machineAllocation[i].StartDate, 1);
                        continue;
                    }

                    if (previousAllocation.EndDate + 1 == machineAllocation[i].StartDate)
                    {
                        continue;
                    }
                    series.Points.AddXY(previousAllocation.EndDate, 0);
                    series.Points.AddXY(machineAllocation[i].StartDate, 1);
                }
                if (previousAllocation != null)
                {
                    series.Points.AddXY(previousAllocation.EndDate, 0);
                }

                utilizationChart.Series.Add(series);


            }

            double interval = -1;

            foreach (ChartArea chartArea in utilizationChart.ChartAreas)
            {
                interval = Math.Max(chartArea.AxisX.Interval,interval);
            }

            foreach (ChartArea chartArea in utilizationChart.ChartAreas)
            {
               chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount; 
               chartArea.AxisX.Interval =  interval;
            }


        }

        private void Validate(SchEduData schEduData, SchEduDataSet.SolutionRow solution)
        {
        }

    }
}
